home *** CD-ROM | disk | FTP | other *** search
- #ifdef DOCUMENTATION
- ******************************* DOCZ Header *********************************
- .MODULE strmcpy
- .LIBRARY csub
- .TYPE function
- .APPLICATION string
- .SYSTEM msdos
- .SYSTEM vms
- .SYSTEM unix
- .AUTHOR Software Toolz
- .DESCRIPTION
- Perform a bounded string copy and produce a NULL terminated string
- .ARGUMENTS
- char *strmcpy(dest,source,maxchar)
- char *dest, /* (w) destination string */
- *source; /* (r) source string */
- int maxchar; /* (r) maximum characters to copy */
- .NARRATIVE
- Strmcpy behaves exactly like strncpy() except that the destination string
- is always NULL-terminated. The "maxchar" argument specifies the size of
- the destination array (NOT including the NULL). If the length of the source
- string exceeds "maxchar" then the last character copied to the destination
- string will be a NULL character.
- .RETURNS
- The address of the destination string.
- .COMMENTS
- This function eliminates a common C pitfall caused by strncpy() producing
- un-terminated strings.
- .LANGUAGE
- VMS, MSDOS: Assembly; UNIX: C
- .FIXES 4/15/88
- Changed documentation to match performance of function.
- Used to be: The "maxchar" argument specifies the size of the destination
- array (including the NULL).
- Is now: The "maxchar" argument specifies the size of the destination
- array (NOT including the NULL).
- .ENDOC END DOCUMENTATION
- *****************************************************************************
- #endif
-
- #include <stdio.h>
- #include <ctype.h>
- #include "csub.h"
-
- /*****************************************************************************
- Strmcpy
- *****************************************************************************/
- char *strmcpy(dest,source,maxchar)
- char *dest, /* (w) destination string */
- *source; /* (r) source string */
- int maxchar; /* (r) maximum characters to copy */
- {
- register i = 0;
-
- while ((source[i]) && (i < maxchar))
- {
- dest[i] = source[i];
- ++i;
- }
-
- dest[i] = CHAR_NUL;
-
- return (dest);
- } /* end strmcpy */
-
-
- #ifdef TESTING /* test program */
-
- /*****************************************************************************
- C test program
- *****************************************************************************/
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char buff[128];
-
- if (argc == 3)
- {
- strmcpy(buff,argv[1],atoi(argv[2]));
- printf("input string length = %d\r\n",strlen(argv[1]));
- printf("input string = (%s)\r\n",argv[1]);
- printf("maximum characters = %d\r\n",atoi(argv[2]));
- printf("result = (%s)\r\n",buff);
- }
- else
- puts("STRMCPY [string] [max. characters]");
- }
-
- #endif /* test program */
-
- /*****************************************************************************
- End STRMCPY.C
- *****************************************************************************/
-
-